﻿Public Class frmCarVeiwer

    Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
        frmCarAED.ShowDialog()
    End Sub

    Private Sub btnEdite_Click(sender As Object, e As EventArgs) Handles btnEdite.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgv.SelectedRows(0)
        Dim frm As New frmCarAED
        frm.txtOperation_No.Text = row.Cells("Operation_No").Value
        frm.cboCarName.Text = row.Cells("Briand_Name").Value
        frm.cboModelName.Text = row.Cells("Model_Name").Value
        frm.txtYear.Text = row.Cells("Yearofcar").Value
        frm.txtMemo.Text = row.Cells("Memo").Value
        frm.cboSA.Text = row.Cells("SA_name").Value
        frm.cboMechanic.Text = row.Cells("Mechanic_name").Value
        frm.txtPlateNo.Text = row.Cells("Plate_Number").Value
        frm.mtxtIn.Text = row.Cells("Check_In").Value
        frm.mtxtOut.Text = row.Cells("Check_out").Value
        frm.cboStatus.Text = row.Cells("Satus").Value
        frm.btnEdite.Text = "កែប្រែ"
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            LoadData()
        End If
    End Sub
    Private Sub LoadData()
        Cmd.Parameters("s") = "%" & txtSearch.Text & "%"
        Me.dgv.DataSource = Cmd.ExecuteDataTable("select C.car_id,Operation_No,Briand_Name,M.Model_Name,Yearofcar,SA_name,Mechanic_name,Plate_Number,Check_In,Check_out,Memo,St.Satus from tblCar C join Model M on M.Model_id  = C.Medel_Id inner join tblBriand B on B.Briand_id = C.Briand_id inner join tblSA S on S.SA_id = C.SA_id inner join tblMechanic Mec on Mec.Mechanic_id = C.Mechanic_id inner join tblSatus St on St.Satus_id = C.Satus_id where Briand_Name like @s and C.Active = 1")

    End Sub
    Private Sub frmCarVeiwer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
        UI.FormatDataGrid(dgv)
    End Sub

    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub rdbRePear_CheckedChanged(sender As Object, e As EventArgs) Handles rdbRePear.CheckedChanged
        Me.dgv.DataSource = Cmd.ExecuteDataTable("select C.car_id,Operation_No,Briand_Name,M.Model_Name,Yearofcar,SA_name,Mechanic_name,Plate_Number,Check_In,Check_out,Memo,St.Satus from tblCar C join Model M on M.Model_id  = C.Medel_Id inner join tblBriand B on B.Briand_id = C.Briand_id inner join tblSA S on S.SA_id = C.SA_id inner join tblMechanic Mec on Mec.Mechanic_id = C.Mechanic_id inner join tblSatus St on St.Satus_id = C.Satus_id WHERE St.Satus_id = 1 ")
    End Sub

    Private Sub rdbQuat_CheckedChanged(sender As Object, e As EventArgs) Handles rdbQuat.CheckedChanged
        Me.dgv.DataSource = Cmd.ExecuteDataTable("select C.car_id,Operation_No,Briand_Name,M.Model_Name,Yearofcar,SA_name,Mechanic_name,Plate_Number,Check_In,Check_out,Memo,St.Satus from tblCar C join Model M on M.Model_id  = C.Medel_Id inner join tblBriand B on B.Briand_id = C.Briand_id inner join tblSA S on S.SA_id = C.SA_id inner join tblMechanic Mec on Mec.Mechanic_id = C.Mechanic_id inner join tblSatus St on St.Satus_id = C.Satus_id WHERE St.Satus_id = 2 ")
    End Sub

    Private Sub rdbReturn_CheckedChanged(sender As Object, e As EventArgs) Handles rdbReturn.CheckedChanged
        Me.dgv.DataSource = Cmd.ExecuteDataTable("select C.car_id,Operation_No,Briand_Name,M.Model_Name,Yearofcar,SA_name,Mechanic_name,Plate_Number,Check_In,Check_out,Memo,St.Satus from tblCar C join Model M on M.Model_id  = C.Medel_Id inner join tblBriand B on B.Briand_id = C.Briand_id inner join tblSA S on S.SA_id = C.SA_id inner join tblMechanic Mec on Mec.Mechanic_id = C.Mechanic_id inner join tblSatus St on St.Satus_id = C.Satus_id WHERE St.Satus_id = 3 ")
    End Sub

    Private Sub cboStatus_SelectedIndexChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("car_id").Value

        If MessageBox.Show("Are You Soure Want to Delete This Item?", "Delete", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Cmd.Parameters("@Id") = id
        Me.dgv.DataSource = Cmd.ExecuteNonQuery("update tblCar set Active = 0 where car_id = @Id")
        LoadData()
    End Sub
End Class